package com.api.gbhg.liuwenbo.version2.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.version2.pojo.BudgetSectionIncome2;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 行政合计
 * @Author lwb
 * @Date 2022.7.21
 */
@Path("gb/bb2")
public class AdministrationApi  extends BudgetSectionIncomeApi2 {

    @POST
    @Path("/getAdministrationList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAdministrationList(Map param){
        Map msgMap = new HashMap();
        if (param == null ||!param.containsKey("nf") ||!param.containsKey("jz")){
            msgMap.put("code","500");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String, Object> paramFormat = paramFormat(param);

        List<BudgetSectionIncome2> czbksyData = null;//所有除缉私局
        List<BudgetSectionIncome2> czbkjsjData = null;//缉私局数据

        try {
            paramFormat.put("dw",getdw());
            czbksyData = CauseApi.AdjustDataStructure(new ArrayList<>(),getCzbksrData(paramFormat));
            paramFormat.put("dw",29);
            czbkjsjData = CauseApi.AdjustDataStructure(new ArrayList<>(),getCzbksrData(paramFormat));
        }catch (Exception e){
            msgMap.put("code","500");
            msgMap.put("msg","程序异常，请联系管理员！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("1",czbksyData);
        map.put("2",czbkjsjData);

        msgMap.put("code","200");
        msgMap.put("msg","");
        msgMap.put("data",map);
        return JSON.toJSONString(msgMap);
    }

    public static String getAlldw(){
        RecordSet rs = new RecordSet();
        String sql = "select dlhsdw as 'dwid'  from uf_dlhsdwgl left join uf_dwsxpz " +
                "on uf_dlhsdwgl.dlhsdw=uf_dwsxpz.xzxtdw ";
        rs.executeQuery(sql);
        List<String> list = new ArrayList<>();
        while (rs.next()){
            list.add(rs.getString("dwid"));
        }
        return String.join(",",list);
    }

    public static String getdw(){
        RecordSet rs = new RecordSet();
        String sql = "select dlhsdw as 'dwid'  from uf_dlhsdwgl left join uf_dwsxpz " +
                "on uf_dlhsdwgl.dlhsdw=uf_dwsxpz.xzxtdw where dwxz=0 and dlhsdw !=29 ";
        rs.executeQuery(sql);
        List<String> list = new ArrayList<>();
        while (rs.next()){
            list.add(rs.getString("dwid"));
        }
        return String.join(",",list);
    }

    public static String getdw(String dw){
        RecordSet rs = new RecordSet();
        String sql = "select dlhsdw as 'dwid'  from uf_dlhsdwgl left join uf_dwsxpz " +
                "on uf_dlhsdwgl.dlhsdw=uf_dwsxpz.xzxtdw where dwxz=0 and dlhsdw !=29 ";
        rs.executeQuery(sql);
        while (rs.next()){
            if (dw.equals(rs.getString("dwid")))
                return dw;
        }
        return "";
    }

    @Override
    public List<BudgetSectionIncome2> getCzbksrData(Map<String, Object> param) {
        List<BudgetSectionIncome2> listBean = new ArrayList<>();

        String nf = param.get("nf").toString();
        String jz = param.get("jz").toString();
        String paramDW = param.get("dw").toString();

        String sql = "select id,gnkmmc,kmdm,"+jz+",yjsr,snjzhpje,yhpje,yqndyjjzje,yjsr,dw from uf_zyczbksr_km where srnf = "+nf+" and dw in ("+paramDW+") ";
        RecordSet rs = new RecordSet();
        //System.err.println("获取财政拨款收入数据 "+sql);
        rs.executeQuery(sql);
        while (rs.next()){
            BudgetSectionIncome2 bean = new BudgetSectionIncome2();
            bean.setId(rs.getString("id"));
            bean.setDataid(rs.getString("id"));
            bean.setKmjxj(0);
            bean.setKmmc(rs.getString("kmdm"));
            bean.setSnjz(rs.getString(jz));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jz)).add(new BigDecimal(rs.getString("yjsr")))));

            String dw = rs.getString("dw");

            List<BudgetSectionIncome2> xjList = getCzbksrEjData(rs.getString("id"), jz,dw,nf);
            if (xjList.size()>0){
                BigDecimal snjzysap = BigDecimal.ZERO;//上年结转预算安排
                BigDecimal bnsrysap = BigDecimal.ZERO;//本年收入预算安排
                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
                BigDecimal snjzyszc = BigDecimal.ZERO; //上年结转预算支出
                BigDecimal bnsryszc = BigDecimal.ZERO; //本年收入预算支出
                BigDecimal snjzyszxl = BigDecimal.ZERO; //上年结转预算执行率
                BigDecimal bnsryszxl = BigDecimal.ZERO; //本年收入预算执行率
                for (BudgetSectionIncome2 beans:xjList){
                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));

                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));

                    snjzyszc = snjzyszc.add(new BigDecimal(beans.getSnjzyszc()));
                    bnsryszc = bnsryszc.add(new BigDecimal(beans.getBnsryszc()));

                    snjzyszxl = snjzyszxl.add(new BigDecimal(beans.getSnjzyszxl()));
                    bnsryszxl = bnsryszxl.add(new BigDecimal(beans.getBnsryszxl()));
                }
                bean.setSnjzysap(snjzysap.toString());
                bean.setBnsrysap(bnsrysap.toString());
                bean.setHj1(snjzysap.add(bnsrysap).toString());

                bean.setSnjzwap(snjzwap.toString());
                bean.setBnsrwap(bnsrwap.toString());
                bean.setHj2(snjzwap.add(bnsrwap).toString());

                bean.setSnjzyszc(snjzyszc.toString());
                bean.setBnsryszc(bnsryszc.toString());
                bean.setHj3(snjzyszc.add(bnsryszc).toString());

                bean.setSnjzyszxl(snjzyszxl.toString());
                bean.setBnsryszxl(bnsryszxl.toString());
                bean.setHj4(snjzyszxl.add(bnsryszxl).toString());

                bean.setXj(xjList);
            }else{
                //已到最下级  取自身值
                bean.setSnjzysap(rs.getString("snjzhpje"));
                bean.setBnsrysap(rs.getString("yhpje"));
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(rs.getString("yqndyjjzje"));
                bean.setBnsrwap(rs.getString("yjsr"));
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                String snbxje = getDfcz_tybxje("0", "1", "0", rs.getString("id"), dw, nf);
                bean.setSnjzyszc(snbxje);
                String bnbxje = getDfcz_tybxje("0", "0", "0", rs.getString("id"), dw, nf);
                bean.setBnsryszc(bnbxje);
                bean.setHj3(String.valueOf(new BigDecimal(bean.getSnjzyszc()).add(new BigDecimal(bean.getBnsryszc()))));

                BigDecimal snzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getSnjzysap()).compareTo(new BigDecimal("0"))>0)
                    snzxl = new BigDecimal(bean.getSnjzyszc()).divide(new BigDecimal(bean.getSnjzysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setSnjzyszxl(snzxl.toString());

                BigDecimal bnzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getBnsrysap()).compareTo(new BigDecimal("0"))>0)
                    bnzxl = new BigDecimal(bean.getBnsryszc()).divide(new BigDecimal(bean.getBnsrysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setBnsryszxl(bnzxl.toString());
                bean.setHj4(String.valueOf(snzxl.add(bnzxl)));
            }

            bean.setName(rs.getString("gnkmmc"));
            listBean.add(bean);
        }
        return listBean;
    }

    /**
     * 参数转换
     * @param param
     * @return
     */
    @Override
    public Map<String,Object> paramFormat(Map<String,Object> param){
        Integer nf = param.get("nf").equals("")? LocalDate.now().getYear():Integer.parseInt(param.get("nf").toString());
        String jz = param.get("jz").equals("")?"yqndyjjzje":param.get("jz").toString();

        param.put("nf",nf);
        param.put("jz",jz);
        return param;
    }
}
